//package com.lz.pricing.framework.config.proxy;
//
//import org.springframework.http.*;
//import org.springframework.http.converter.StringHttpMessageConverter;
//import org.springframework.stereotype.Component;
//import org.springframework.util.MultiValueMap;
//import org.springframework.util.StreamUtils;
//import org.springframework.web.client.RestTemplate;
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.nio.charset.StandardCharsets;
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.PrintWriter;
//import java.net.URI;
//import java.util.Collections;
//import java.util.List;
//
//
//@WebFilter(filterName = "prod-api", urlPatterns = "/prod-api/*")
//@Component
//public class xeqProxyimplements implements Filter {
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
////        if(config.getType())
//        if (false){
//            HttpServletRequest req = (HttpServletRequest) servletRequest;
//            HttpServletResponse resp = (HttpServletResponse) servletResponse;
//
//            String uri = req.getRequestURI();
//
//            if (uri.startsWith("/prod-xir-api/")) {
//                //String xirApi = config.getXirApi();
//                String xirApi = "http://191.168.0.203:8099";
//                String replace = uri.replace("/prod-xir-api", "");
//                String redirectUrl = xirApi + replace;
////            servletRequest.getRequestDispatcher(xirApi + replace).forward(servletRequest, servletResponse);
//                // 3、请求类型
//                String method = req.getMethod();
//                HttpMethod httpMethod = HttpMethod.resolve(method);//method
//                // 4、请求头
//                MultiValueMap<String, String> headers = parseRequestHeader(req);//header
//                // 5、请求体
//                byte[] body = parseRequestBody(req);//body
//
//                //  6、封装发singhttp请求
//                RequestEntity requestEntity = new RequestEntity(body, headers, httpMethod, URI.create(redirectUrl));
//                RestTemplate restTemplate = new RestTemplate();
//                //编码格式转换
//                restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
//                ResponseEntity<String> result = restTemplate.exchange(requestEntity, String.class);
//
//                // 将转发请求得到的结果和响应头返回客户端
//                String resultBody = result.getBody();
//                HttpHeaders resultHeaders = result.getHeaders();
//                MediaType contentType = resultHeaders.getContentType();
//                if (contentType != null) {
//                    resp.setContentType(contentType.toString());
//                }
//                resp.setCharacterEncoding("UTF-8");// 在getWriterz之前执行，否则不生效
//                PrintWriter writer = resp.getWriter();
//                writer.write(resultBody);
//                writer.flush();
//
//
//            } else {
//                String replace = uri.replace("/prod-api", "");
//                servletRequest.getRequestDispatcher(replace).forward(servletRequest, servletResponse);
//            }
//        }
//    }
//    /**
//     * request body
//     * @param request
//     * @return
//     * @throws IOException
//     */
//    private byte[] parseRequestBody(HttpServletRequest request) throws IOException {
//        InputStream inputStream = request.getInputStream();
//        return StreamUtils.copyToByteArray(inputStream);
//    }
//
//    /**
//     * request header
//     * @param request
//     * @return
//     */
//    private MultiValueMap<String, String> parseRequestHeader(HttpServletRequest request) {
//        HttpHeaders httpHeaders = new HttpHeaders();
//        List<String> headerNames = Collections.list(request.getHeaderNames());
//        for(String headerName : headerNames) {
//            List<String> headerValues = Collections.list(request.getHeaders(headerName));
//            for (String headerValue : headerValues) {
//                httpHeaders.add(headerName, headerValue);
//            }
//        }
//        return httpHeaders;
//    }
//
//
//}